package com.iwhalecloud.bss.kite.cucc.web.service.accept.handler.attr;

import com.iwhalecloud.bss.kite.client.dto.DataResult;
import com.iwhalecloud.bss.kite.client.vo.scene.CommonAttrVO;
import com.iwhalecloud.bss.kite.common.constant.ErrorConsts;
import com.iwhalecloud.bss.kite.common.constant.KeyConsts;
import com.iwhalecloud.bss.kite.common.util.KiteStringUtils;
import com.iwhalecloud.bss.kite.cucc.common.util.DateFormatUtils;
import com.iwhalecloud.bss.kite.manager.api.IQuerySceneInstService;
import com.iwhalecloud.bss.kite.web.service.accept.attr.handler.AbstractAttrHandler;
import com.ztesoft.bss.common.exception.BssException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.format.DateTimeFormatter;

/**
 * @Classname AllCompleteTimeGJHandler
 * @Description 全程要求完成时间-国际产品
 *    初始化默认：当前时间+30天
 *    不能小于当前时间
 * @Author zhang.song
 * @Date 2022-04-01 14:46
 */
@Service
public class AllCompleteTimeGJHandler extends AbstractAttrHandler {

    @Autowired
    private IQuerySceneInstService querySceneInstService;

    @Override
    public void init(CommonAttrVO commonAttr) {
        if(KiteStringUtils.isNotEmpty(commonAttr.getValue())){
            return;
        }

        LocalDateTime nowTime = LocalDateTime.now();
        nowTime = LocalDateTime.of(nowTime.getYear(), nowTime.getMonthValue(), nowTime.getDayOfMonth(), 23, 59, 59);
        nowTime = nowTime.plusDays(30);

        DateTimeFormatter dtf = DateTimeFormatter.ofPattern(DateFormatUtils.getDateFormatTypeByAttrType(commonAttr.getAttrValueTypeId()));
        commonAttr.setValue(dtf.format(nowTime));
        setCommonAttr(commonAttr);
    }

    /**
     * 不能小于当前时间
     * @author zhang.song
     * @date 2022-04-01 15:17
     */
    @Override
    public DataResult<Boolean> apply(String sceneInstId, String fieldName, String value, String valueDesc) {
        // 是否为页面初始化渲染，是（T），跳过校验
        String isInitPageRenderFlag = querySceneInstService.getExtParameterValue(sceneInstId, "isInitPageRenderFlag");
        if (KeyConsts.IFTRUE_T.equals(isInitPageRenderFlag)) {
            return super.apply(sceneInstId, fieldName, value, valueDesc);
        }

        if(KiteStringUtils.isNotEmpty(value) && value.length() > 9) {
            DateTimeFormatter dtf = DateTimeFormatter.ofPattern(DateFormatUtils.DATE_TIME_FORMAT.substring(0, value.length()));
            LocalDateTime localDateTime = null;
            if(value.length() == 10) {
                try {
                    LocalDate localDate = LocalDate.parse(value, dtf);
                    localDateTime = LocalDateTime.of(localDate, LocalTime.of(0, 0, 0));
                } catch (Exception e) {
                    ErrorConsts.BUSI_COMMON_ERROR.throwOut("日期转换异常");
                }
            } else {
                try {
                    localDateTime = LocalDateTime.parse(value, dtf);
                } catch (Exception e) {
                    ErrorConsts.BUSI_COMMON_ERROR.throwOut("日期转换异常");
                }
            }

            if(localDateTime != null) {
                LocalDateTime nowTime = LocalDateTime.now();
                if(localDateTime.isBefore(nowTime)) {
                    BssException.throwOut("", "", "全程要求完成时间不能小于当前时间");
                }
            }
        }
        return super.apply(sceneInstId, fieldName, value, valueDesc);
    }
}
